<?php


namespace lkeme\BiliHelper;


class Tuuz
{

    public static function run()
    {
        if (!socket_set_nonblock(Index::$socket))
            die('Unable to set nonblocking mode for socket');
        $res = socket_read(Index::$socket, 1460);
        echo $res;
        $rtt = json_decode($res);

//        return self::route($rtt);
    }

    public static function route($res)
    {

        $type = $res["type"];
        $data = $res["data"];
        $echo = $res["echo"];
        switch ($type) {
            case "notam":
            case "login":
                echo $data;
                echo "\n";
                break;

            case "config":
                return self::config($data);

            default:
                echo $data;
                echo $echo;
                echo "\n";
                break;
        }
    }

    public static function config($config)
    {
        if ($config) {
            foreach ($config as $key => $value) {
                File::writeNewEnvironmentFileWith($key, $value, false);
            }
//            Log::notice("Config配置已经修改");
            return true;
        } else {
            Log::info("Config格式不正确无法读取");
        }
        return false;
    }

    public static function conf_write($key, $value)
    {
        $msg = [
            "type" => "conf_write",
            "data" => [
                $key => $value,
            ],
            "echo" => "conf_write",
        ];
        socket_write(Index::$socket, json_encode($msg, 320), 1460);
    }

    public static function log_write($type, $message)
    {
        $msg = [
            "type" => "log_write",
            "data" => [
                "type" => $type,
                "message" => $message,
            ],
            "echo" => "log_write",
        ];
        socket_write(Index::$socket, json_encode($msg, 320), 1460);
    }

    public static function register()
    {
        echo "请输入APP登录码\n";
        $stdin = fopen('php://stdin', 'r');
        $code = trim(fgets($stdin));
        $msg = [
            "type" => "register_client",
            "data" => [
                "code" => $code,
            ],
            "echo" => "register_client",
        ];
        socket_write(Index::$socket, json_encode($msg, 320), 1460);
        $res = socket_read(Index::$socket, 1460);

        $ret = json_decode($res, 1);
        if ($ret["type"] == "register" && $ret["code"] == 0) {

            echo($ret["data"]);
            echo "\n";
            //等待配置文件加载
            echo "验证成功等待配置文件加载……\n";
            return self::wait_for_login();
        } else {
            echo($ret["data"]);
            echo "\n";
            usleep(1000000);
            self::register();
        }
    }

    public static function wait_for_login()
    {
        $res = socket_read(Index::$socket, 1460);
        $ret = json_decode($res, 1);
        if ($ret["type"] == "config" && $ret["code"] == 0) {
            $data = $ret["data"];
            return self::config($data);
        } else {
            sleep(1);
            echo "等待文件";
            echo "\n";
            self::wait_for_login();
            return false;
        }
    }

    public static function socket()
    {
        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        socket_connect($socket, '127.0.0.1', 82);

        return $socket;
    }
}